package com.deep.transaction.demo5.dao.impl;


import com.deep.transaction.demo5.dao.User2Dao;
import com.deep.transaction.demo5.pojo.User2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

//IOC,将资源存放到spring容器
@Repository
public class User2DaoImpl implements User2Dao {
    /*
       DI：将资源从spring容器中注入到Java代码中
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void outMoney(String outName, Integer money) {
        jdbcTemplate.update(
                "update t_user2 set money = money - ? where username = ?",
                money,
                outName
        );
    }

    public void inMoney(String inName, Integer money) {
        jdbcTemplate.update(
                "update t_user2 set money = money + ? where username = ?",
                money,
                inName
        );
    }

    public int addUser(User2 inputUser) throws Exception {
        return jdbcTemplate.update(
                "insert into t_user2 values(?,?,?,?,?,?)",
                inputUser.getId(),
                inputUser.getUsername(),
                inputUser.getPassword(),
                inputUser.getAge(),
                inputUser.getSex(),
                inputUser.getMoney()
        );
    }


    public int deleteUser(Integer userId) throws Exception {
        return jdbcTemplate.update(
                "delete from t_user2 where id = ?",
                userId
        );
    }

    public int updateUser(User2 inputUser) throws Exception {
        return jdbcTemplate.update(
                "update t_user2 set username=?,password=?,age=?,sex=?,money=? where id=?",
                inputUser.getUsername(),
                inputUser.getPassword(),
                inputUser.getAge(),
                inputUser.getSex(),
                inputUser.getMoney(),
                inputUser.getId()
        );
    }


    public User2 selectUserById(Integer userId) throws Exception {
        return jdbcTemplate.queryForObject(
                "select * from t_user2 where userId= ?",
                new RowMapper<User2>() {
                    public User2 mapRow(ResultSet rs, int rowNum) throws SQLException {
                        User2 user = new User2();
                        user.setId(rs.getInt("id"));
                        user.setUsername(rs.getString("username"));
                        user.setPassword(rs.getString("password"));
                        user.setAge(rs.getInt("age"));
                        user.setSex(rs.getString("sex"));
                        user.setMoney(rs.getInt("money"));
                        return user;
                    }
                },
                userId
        );
    }

    public List<User2> selectUser() throws Exception {
        return jdbcTemplate.query(
                "select * from t_user2",
                new BeanPropertyRowMapper<User2>(User2.class)
        );
    }
}
